package com.project.util;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
 * http://ask.csdn.net/questions/27703
 * http://download.csdn.net/detail/c2243261/9146711
 * http://www.52itstyle.com/forum.php?mod=viewthread&tid=5073
 * @author gxs
 * 	
 * 
 *	public static String HOST = "smtp.sina.com";
	public static String FROM = "scueasybill@sina.com";
	public static String MAILUSER = "scueasybill@sina.com";
	public static String MAILPWD = "easybill";
 * 
 * 
 * 
 */
public class SendMail {
	private String seconds = StringUtil.SCEONDS;
	private String host = StringUtil.HOST;
	private String from = StringUtil.FROM;
	private String user = StringUtil.MAILUSER;
	private String pwd = StringUtil.MAILPWD;
	// 获取6位随机验证码
	public static String getRandom() {
		String num = "";
		for (int i = 0; i < 6; i++) {
			num = num + String.valueOf((int) Math.floor(Math.random() * 9 + 1));
		}
		return num;
	}
	
	public static boolean send(String txt, String to, String subject) {
		 String seconds = StringUtil.SCEONDS;
		 String host = StringUtil.HOST;
		 String from = StringUtil.FROM;
		 String user = StringUtil.MAILUSER;
		 String pwd = StringUtil.MAILPWD;
		Properties props = new Properties();
		// 设置发送邮件的邮件服务器的属性（这里使用网易的smtp服务器）
		props.put("mail.smtp.host", host);
		// 需要经过授权，也就是有户名和密码的校验，这样才能通过验证（一定要有这一条）
		props.put("mail.smtp.auth", "true");
		// 用刚刚设置好的props对象构建一个session
		Session session = Session.getDefaultInstance(props);
		// 有了这句便可以在发送邮件的过程中在console处显示过程信息，供调试使
		// 用（你可以在控制台（console)上看到发送邮件的过程）
		session.setDebug(true);
		// 用session为参数定义消息对象
		MimeMessage message = new MimeMessage(session);
		try {
			// 加载发件人地址
			message.setFrom(new InternetAddress(from));
			// 加载收件人地址
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			// 加载标题
			message.setSubject(subject);
			// 向multipart对象中添加邮件的各个部分内容，包括文本内容和附件
			Multipart multipart = new MimeMultipart();
			// 设置邮件的文本内容
			BodyPart contentPart = new MimeBodyPart();

			// 加上"text/html; charset=utf-8"，表示支持html格式的邮件
			contentPart.setContent(txt, "text/html; charset=utf-8");

			multipart.addBodyPart(contentPart);
			message.setContent(multipart);
			// 保存邮件
			message.saveChanges();
			// 发送邮件
			Transport transport = session.getTransport("smtp");
			// 连接服务器的邮箱
			transport.connect(host, user, pwd);
			// 把邮件发送出去
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(getRandom());
		send("test: "+getRandom(), "972042723@qq.com", "object");
	}

}
